package com.itshidu.jiaowu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itshidu.jiaowu.dao.ClassesMapper;
import com.itshidu.jiaowu.dao.StudentClassesMapper;
import com.itshidu.jiaowu.dao.UserMapper;
import com.itshidu.jiaowu.entity.Classes;
import com.itshidu.jiaowu.entity.Student;
import com.itshidu.jiaowu.dao.StudentMapper;
import com.itshidu.jiaowu.entity.StudentClasses;
import com.itshidu.jiaowu.entity.User;
import com.itshidu.jiaowu.service.StudentService;
import com.itshidu.jiaowu.util.LoginUtil;
import com.itshidu.jiaowu.util.Result;
import com.itshidu.jiaowu.vo.StudentVo;
import com.itshidu.web.tools.MD5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-08.
 */
@Service
@Primary
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired UserMapper usermapper;
    @Autowired StudentMapper studentmapper;
    @Autowired ClassesMapper classesmapper;
    @Autowired StudentClassesMapper studentClassesMapper;



    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public StudentVo student(StudentVo studentvo) {
        User u=(User) LoginUtil.get("UserInfo");
        User user=usermapper.selectById(u.getId());
        Student s=studentmapper.selectById(u.getId());
        BeanUtils.copyProperties(s,studentvo);

        studentvo.setUsername(user.getUsername());
        studentvo.setSex(user.getSex());
        studentvo.setBirthday(user.getBirthday());
        studentvo.setIdcCode(user.getIdcCode());
        studentvo.setAddress(user.getAddress());
        studentvo.setPhone(user.getPhone());
        studentvo.setQq(user.getQq());
        studentvo.setWx(user.getWx());
        studentvo.setEmail(user.getEmail());
        studentvo.setSite(user.getSite());
        LoginUtil.set("Student",s);
        return studentvo;
    }

    @Override
    public Result save(User user,String college_name,String graduate_date,String join_date,String parent_info,String idc_code,String classname) {
        if(user.getUsername().equals("")){
            return Result.of(250,"用户名不能为空！！");   //判断用户名是否为空
        }
        User uu=usermapper.SelectByName(user.getUsername());
        if(uu!=null){
            return Result.of(100,"用户名已存在！！");   //判断用户名是否存在
        }
        try {
        String salt=UUID.randomUUID().toString();           //随机生成UUID作为盐
        String pass="123456";                               //默认密码是123456
        String pwd = MD5Util.md5(pass+salt);           //md5加密
        user.setSalt(salt);
        user.setPassword(pwd);
        user.setIdcCode(idc_code);
        user.setIdcConfirm(2);                              //默认为2未实名认证
        user.setAvatar("de.png");                           //默认为de.png头像
        user.setType("student");                            //默认type为student
        usermapper.insert(user);                            //添加user对象
        Student student=new Student();
        User  d=usermapper.SelectByName(user.getUsername());          //根据名字查user对象.
        student.setId(d.getId());                           //设置学生id
        student.setScore(100);                              //默认积分为100
        student.setCollegeName(college_name);
        Date grdate=sdf.parse(graduate_date);
        Date jdate=sdf.parse(join_date);
        student.setGraduateDate(grdate);
        student.setJoinDate(jdate);
        student.setParentInfo(parent_info);
        Classes classes=classesmapper.selectByName(classname);
        Long idd= classes.getId();
        student.setClassid(idd);
        studentmapper.insert(student);                      //添加student对象

        StudentClasses stucla = new StudentClasses();
        Date joindate = sdf.parse(join_date);
        stucla.setJoinDate(joindate);
        stucla.setOutDate(null);
        stucla.setClassesId(classes.getId());
        stucla.setStudentId(student.getId());
        studentClassesMapper.insert(stucla);                //学生-班级 中间表同步插入一条信息

        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.of(200,"学生信息添加成功");
    }

    @Override
    public Result xiugai(User user,String college_name,String graduate_date,String join_date,String parent_info,String idc_code,String site) {
            try {
                User u = (User) LoginUtil.get("UserInfo");
                User user1 = usermapper.selectById(u.getId());
                user1.setUsername(user.getUsername());                         //设置名字
                user1.setSex(user.getSex());                                   //设置性别
                user1.setAddress(user.getAddress());                           //设置现住址
                user1.setBirthday(user.getBirthday());                         //设置生日
                user1.setIdcCode(idc_code);                                     //设置身份证
                user1.setSite(site);                                            //设置籍贯
                user1.setPhone(user.getPhone());                               //设置电话
                user1.setEmail(user.getEmail());                               //设置邮箱
                user1.setQq(user.getQq());                                     //设置QQ
                user1.setWx(user.getWx());                                     //设置微信
                System.out.println("stue================"+user1.getSite());
                usermapper.updateById(user1);                        //更新user对象

                Student s = (Student) LoginUtil.get("Student");
                Student student1 = studentmapper.selectById(s.getId());
                student1.setCollegeName(college_name);               //设置学生毕业学校的名字
                Date Jdate = sdf.parse(join_date);
                student1.setJoinDate(Jdate);                         //设置来校日期
                Date Gdate=sdf.parse(graduate_date);
                student1.setGraduateDate(Gdate);                    //设置学历毕业日期
                student1.setParentInfo(parent_info);                 //设置学生的家长信息
                studentmapper.updateById(student1);
                LoginUtil.set("Student", student1);

                StudentVo vo=new StudentVo();
                BeanUtils.copyProperties(user1,vo);
                BeanUtils.copyProperties(student1,vo);
            }catch (Exception e){
                e.printStackTrace();
            }
        return Result.of(100);
    }

    //某个班次 学生的管理 查询所有学生
    @Override
    public Result selectall(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);                                   //开始分页
        QueryWrapper<Student> studentwrapper = new QueryWrapper<>();      //mybatis的Wrapper接口
        List<Student> list=studentmapper.selectList(studentwrapper);
        PageInfo<Student> info = new PageInfo(list);
        List<StudentVo> list1=new ArrayList();
        for(Student s:list){
        Long Sid=s.getId();
        User user=usermapper.selectById(Sid);
        String sex=user.getSex();
        String name=user.getUsername();
        Long Cid=s.getClassid();
        Classes classes=classesmapper.selectById(Cid);
        Integer stage=classes.getStage();
        String nn=classes.getName();
        StudentVo sv=new StudentVo();
        sv.setId(s.getId());
        sv.setClassid(s.getClassid());
        sv.setUsername(name);
        sv.setSex(sex);
        sv.setStage(stage);
        sv.setClassesname(nn);
        list1.add(sv);
        }
        return Result.of().put("code", 0)
                .put("msg", "")
                .put("count",info.getTotal())
                .put("data", list1);
    }
}
